import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class implements a simple phone GUI. When creating a new object of this
 * class the GUI components, such as buttons, labels, etc. are just displayed.
 * They do not yet react on user actions, such as pressing a button, etc.
 * 
 * @author H. Diethelm HTA Luzern, Christian Heldstab ZbW
 * @version 1.1 12.10.2006
 */
public class JavaPhoneGUI{

	// Main Frame
	JFrame mainFrame = new JFrame("Java Phone");
	
	// Declare and create key buttons
	JButton key1 = new JButton("1");
	JButton key2 = new JButton("2");
	JButton key3 = new JButton("3");
	JButton key4 = new JButton("4");
	JButton key5 = new JButton("5");
	JButton key6 = new JButton("6");
	JButton key7 = new JButton("7");
	JButton key8 = new JButton("8");
	JButton key9 = new JButton("9");
	JButton key0 = new JButton("0");
	JButton keyR = new JButton("#");
	JButton keyS = new JButton("*");
	JButton keyHookoff = new JButton("Hook off");

	// Declare and create other GUI elements
	JTextField display = new JTextField("0319994263");
	JLabel state = new JLabel("ready");
	

	// Declare and create panels
	JPanel keyPanel = new JPanel();
	JPanel keyDisplayPanel = new JPanel();
	JPanel stateHookoffPanel = new JPanel();


	// paints the frame and all widgets on it
	public void paint() {
		// Initialise frame and GUI elements
		mainFrame.setBackground(Color.lightGray);
		mainFrame.setResizable(false);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		display.setEditable(false);

		// Set layout of all panels and frames
		keyPanel.setLayout(new GridLayout(4, 3, 20, 20));
		keyDisplayPanel.setLayout(new BorderLayout(20, 20));
		stateHookoffPanel.setLayout(new BorderLayout(20, 20));
		mainFrame.setLayout(new BorderLayout(20, 20));
		state.setForeground(Color.green);

		// Add key buttons to key panel
		keyPanel.add(key1);
		keyPanel.add(key2);
		keyPanel.add(key3);
		keyPanel.add(key4);
		keyPanel.add(key5);
		keyPanel.add(key6);
		keyPanel.add(key7);
		keyPanel.add(key8);
		keyPanel.add(key9);
		keyPanel.add(key0);
		keyPanel.add(keyR);
		keyPanel.add(keyS);
		
		// Declare ActionListeners for key buttons
		key1.addActionListener(new KeyListener());
		key2.addActionListener(new KeyListener());
		key3.addActionListener(new KeyListener());
		key4.addActionListener(new KeyListener());
		key5.addActionListener(new KeyListener());
		key6.addActionListener(new KeyListener());
		key7.addActionListener(new KeyListener());
		key8.addActionListener(new KeyListener());
		key9.addActionListener(new KeyListener());
		key0.addActionListener(new KeyListener());
		keyR.addActionListener(new KeyListener());
		keyS.addActionListener(new KeyListener());
		keyHookoff.addActionListener(new HookListener());

		// Assemble key/display panel
		keyDisplayPanel.add(display, BorderLayout.NORTH);
		keyDisplayPanel.add(keyPanel, BorderLayout.CENTER);

		// Assemble hook/state panel
		stateHookoffPanel.add(state, BorderLayout.NORTH);
		stateHookoffPanel.add(keyHookoff, BorderLayout.CENTER);

		// Add key/display panel and hook/state panel to frame
		mainFrame.add(keyDisplayPanel, BorderLayout.CENTER);
		mainFrame.add(stateHookoffPanel, BorderLayout.WEST);
		mainFrame.setSize(300, 300);
		mainFrame.setVisible(true);
	}

	
	/**
	 * Main method to start the application
	 */
	class KeyListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ziffer = e.getActionCommand();
			display.setText(display.getText() + ziffer);
		}
	}
	
	class HookListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (keyHookoff.getText() == "Hook off") {
				keyHookoff.setText("Hook on");
				state.setText("connected");
			} else {
				keyHookoff.setText("Hook off");
				state.setText("ready");
				display.setText("");
			}
		}
	}
	
	public static void main(String[] args) {
		JavaPhoneGUI gui = new JavaPhoneGUI();
		gui.paint();
	}
}